package util

import (
	"io"
	"strings"
)

// A well-behaved substitution for fmt.Fscanln.
// The reader is recommended to be a buffered reader.
// Note that
//   1. trailing \r is ignored
//   2. ...[\n][EOF] and ...[EOF] are not distinguished.
//
func WellBehavedFscanln(f io.Reader) (string, int, error) {
	var buf strings.Builder
	var collect = func() string {
		var line = buf.String()
		if len(line) > 0 && line[len(line)-1] == '\r' {
			line = line[:len(line)-1]
		}
		return line
	}
	var total = 0
	var one_byte = [] byte { 0 }
	for {
		var n, err = f.Read(one_byte)
		total += n
		if err != nil {
			if err == io.EOF && buf.Len() > 0 {
				return collect(), total, nil
			} else {
				return "", total, err
			}
		}
		if rune(one_byte[0]) != '\n' {
			buf.WriteByte(one_byte[0])
		} else {
			return collect(), total, nil
		}
	}
}


